package com.example.myimageshare.View;


import android.content.Context;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.myimageshare.R;
import com.example.myimageshare.adapter.HomeAdapter;
import com.example.myimageshare.domain.UserArt;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SQLQueryListener;


/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {

    //适配器
    private HomeAdapter adapter = null;

    //RecyclerView对象
    private RecyclerView recyclerView = null;
    //SwipeRefreshLayout对象
    private SwipeRefreshLayout swipe = null;
//    数据对象
    List<UserArt> users = new ArrayList<>();
//  上下文
    AppCompatActivity context;
    View rootView;
    LinearLayoutManager llm;
    GridLayoutManager glm;
    ImageView imageView = null;

    public HomeFragment(AppCompatActivity context) {
        // Required empty public constructor
        this.context = context;
    }

    @Override
    public void onCreate(Bundle savadInstanceState){
        super.onCreate(savadInstanceState);

    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
//        context = getActivity();
        final View rootView = inflater.inflate(R.layout.fragment_home,
                container,false);
//        llm = new LinearLayoutManager(context);
        glm = new GridLayoutManager(context,2);
        recyclerView = rootView.findViewById(R.id.lv_img_home_list);
//        recyclerView.setLayoutManager(llm);
        recyclerView.setLayoutManager(glm);
        swipe = rootView.findViewById(R.id.swipe);


        swipe.setRefreshing(true);
        initData();
        swipe.setEnabled(true);

        /*下拉触发事件*/
        swipe.setOnRefreshListener(
                new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        initData2();
                    }
                }
        );



        return rootView;
    }

    public void initData(){
        RefreshData();

    }
    public void initData2(){
        RefreshData();
        adapter.notifyDataSetChanged();
    }


    public void RefreshData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                BmobQuery<UserArt> bmobQuery = new BmobQuery<UserArt>();
                bmobQuery.setLimit(50);
                bmobQuery.setSQL("select * from UserArt");
                bmobQuery.doSQLQuery(new SQLQueryListener<UserArt>(){
                    @Override
                    public void done(BmobQueryResult<UserArt> bmobQueryResult, BmobException e) {
                        if (e == null) {
                            users = new ArrayList<>();
                            users = bmobQueryResult.getResults();
                            if(null!=users&&users.size()>0) {
                                adapter = new HomeAdapter(
                                        context,
                                        R.layout.item_layout, users);
                                recyclerView.setAdapter(adapter);
                                swipe.setRefreshing(false);

                                System.out.println("查询成功");
                            }else {
                                System.out.println("查询成功,无数据返回");
                                adapter = new HomeAdapter(
                                        context,
                                        R.layout.item_layout, null);
                                recyclerView.setAdapter(adapter);
                                swipe.setRefreshing(false);
                            }

                        } else {
                            adapter = new HomeAdapter(
                                    context,
                                    R.layout.item_layout, null);
                            recyclerView.setAdapter(adapter);
                            swipe.setRefreshing(false);
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        }).start();
    }
}
